package cn.chenshiwu121.controller;

import cn.chenshiwu121.pojo.Content;
import cn.chenshiwu121.pojo.EasyUIDataGridResult;
import cn.chenshiwu121.pojo.TaotaoResult;
import cn.chenshiwu121.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/content")
public class ContentController {

    @Autowired
    private ContentService service;

    @RequestMapping(value = "query/list", method = RequestMethod.GET)
    @ResponseBody
    public EasyUIDataGridResult<Content> getContentList(Long categoryId, Integer page, Integer rows) {
        return service.getContentList(categoryId, page, rows);
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult saveContent(Content content) {
        TaotaoResult result = null;
        try {
            service.saveContent(content);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult deleteContent(String ids) {
        TaotaoResult result = null;
        try {
            service.deleteContentByIds(ids);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult editContent(Content content) {
        TaotaoResult result = null;
        try {
            service.editContent(content);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

}
